//
//  LBCategoryApplication.m
//  jslj
//
//  Created by luckyRoy on 2017/10/13.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategoryApplication.h"
#import "LBCategoryViewController.h"
#import <JLRoutes+LBRouter.h>
#import <MJExtension.h>

NSString *const kCategoryApplicationScheme = @"jsljCategoryCenter://";

LBCategoryApplication *shareApplication = nil;

@interface LBCategoryApplication ()

@end

@implementation LBCategoryApplication

+ (void)openURL:(NSString *)urlStr callBack:(LBRouteCallBackBlock)callBack
{
    NSString *blockAdd = [JLRoutes addCallBackBlock:callBack];
    urlStr = [urlStr stringByAppendingString:[NSString stringWithFormat:@"/%@",blockAdd]];
    LBCategoryOpen(urlStr);
}

+ (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    if ([url.absoluteString containsString:kCategoryApplicationScheme]){
        return [[JLRoutes routesForScheme:kCategoryApplicationScheme] routeURL:url];
    }
    return NO;
}

+ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[JLRoutes routesForScheme:kCategoryApplicationScheme] addRoute:@"category/selected/:codeType/:codeStrcing/:isSingleSelected/:maxSelectedLevel/:blockKey" handler:^BOOL(NSDictionary<NSString *,id> * _Nonnull parameters) {
        NSInteger codeType = [parameters[@"codeType"]integerValue];
        NSInteger maxSelectedLevel = [parameters[@"maxSelectedLevel"]integerValue];
        NSString *codeStrcing = parameters[@"codeStrcing"];
        NSString *blockKey = parameters[@"blockKey"];
        BOOL isSingleSelected = [parameters[@"isSingleSelected"]boolValue];
        
        NSArray *codeArray = nil;
        if (codeStrcing.length && [codeStrcing integerValue]) {
            //所有codeString以','组合分割
            codeArray = [codeStrcing componentsSeparatedByString:@","];
        }
        LBCategoryViewController *categoryViewController = [[LBCategoryViewController alloc] initWithType:codeType chooseArray:codeArray isSingleSelected:isSingleSelected maxSelectedLevel:maxSelectedLevel complete:^(NSArray<LBCategoryModel *> *selectedArray) {
//            NSMutableArray *resultArray = [NSMutableArray array];
//            [selectedArray enumerateObjectsUsingBlock:^(LBCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                NSDictionary *dic = [obj mj_keyValues];
//                [resultArray addObject:dic];
//            }];
            LBRouteCallBackBlock callBlock = [JLRoutes getCallBackWithKey:blockKey];
            if (callBlock) {
                callBlock(selectedArray);
            }
        }];
        
        UINavigationController *currentNavViewController = [UIApplication getCurrentViewController].navigationController;
        
        if (!currentNavViewController) {
            currentNavViewController = [UIApplication sharedApplication].keyWindow.rootViewController ;
        }
        
        [currentNavViewController pushViewController:categoryViewController animated:YES];

        return YES;
    }];
    
    return YES;
}

@end
